package server;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private String method;//请求方式
    private String url;//请求资源路径
    private InputStream inputStream;//输入流

    public Request() {

    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        //因为网络特性，有可能请求到了，但数据还没到，那么inputStream.available()可能等于0
        while (count == 0) {
            count = this.inputStream.available();
        }
        byte[] bytes = new byte[count];
        //把流当中的数据，读取到数组
        this.inputStream.read(bytes);
        String ret = new String(bytes);
        System.out.println("====>请求信息：" + ret);

        //获取第一行：请求头
        String firstLine = ret.split("\\n")[0];
        this.method = firstLine.split(" ")[0];
        this.url = firstLine.split(" ")[1];
        System.out.println("method:"+method+" url:"+url);
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
